//
//  MainTabBarController.m
//  时光电影项目
//
//  Created by 墨狂之逸才 on 15/7/17.
//  Copyright (c) 2015年 墨狂之逸才. All rights reserved.
//

#import "MainTabBarController.h"
#import "MainViewController.h"
#import "NewsViewController.h"
#import "TopViewController.h"
#import "CinemaViewController.h"
#import "MoreViewController.h"
#import "MyButton.h"
#import "BaseNavigationController.h"
@interface MainTabBarController ()

{
    UIImageView *selectedImage;
}
@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //添加子控件,放到标签控制器
    //一旦有viewcontroler添加进入标签控制器，标签控制器就会自动在tabBar上放置对应的按钮数，
    [self _creatSubView];
    
    //然后先移除tabBar上面的控件，在添加自己的子控件在上面，
    //如果顺序有误，先创建按钮的话，移除tabBar上的按钮就无效（因为还没有），然后添加viewcontroller进去，就又创建了tabbar上的按钮，然后就是原来tabbar的按钮覆盖在自己创建的按钮上了
    [self _creatTabBar];
//    self.tabBar.items =
//    UIBarButtonItem
    
    
    // Do any additional setup after loading the view.
}


//移除子视图
- (void)_creatTabBar
{
    //获得tabBar上的子视图
    for (UIView *subView in self.tabBar.subviews) {
        
        //获得UITabBarButton这个类
        Class cls = NSClassFromString(@"UITabBarButton");
        
        
        //UITabBarItem －－－》model纪录了数据
        //数据交给UITabBarButton 显示
        if ([subView isKindOfClass:cls]  ) {
            
            //移除子视图从tabBar
            [subView removeFromSuperview];
        }
        
    }

    NSArray *imageArray = [NSArray array];
    imageArray = @[@"movie_home",@"msg_new@2x",@"start_top250",@"icon_cinema",@"more_setting"];
    
    NSArray *nameArray = [NSArray array];
    nameArray = @[@"首页",@"新闻",@"Top250",@"影院",@"更多"];
    
    
//创建自定义按钮
    CGFloat width = CGRectGetWidth(self.tabBar.frame)/5;
    CGFloat height = CGRectGetHeight(self.tabBar.frame);
    
//    for (int i = 0; i<5; i++) {
//        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(i*width, 0, width, height)];
//        
//        [button setTitle:nameArray[i] forState:UIControlStateNormal];
//        [button setImage:[UIImage imageNamed:imageArray[i]] forState:UIControlStateNormal];
//        button.titleLabel.font = [UIFont systemFontOfSize:11];
//        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        
//        //调整image和title的位置
//        button.titleEdgeInsets = UIEdgeInsetsMake(30, -18, 0, 0);
//        button.imageEdgeInsets = UIEdgeInsetsMake(-10, 20, 0, 0);
//        
//    
//        [self.tabBar addSubview:button];
//    }
    
    
    
    
    //添加tabBar的背景颜色
    UIImageView *backgroundImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width*5, height)];
    backgroundImage.image = [UIImage imageNamed:@"nav_bg_all-64@2x"];
    [self.tabBar addSubview:backgroundImage];
    
    //根据图片的原来大小放置
//    self.tabBar.backgroundImage = [UIImage imageNamed:@"nav_bg_all-64@2x"];
    
    
    
    //添加按钮背景图片
    selectedImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, height)];
    selectedImage.image = [UIImage imageNamed:@"selectTabbar_bg_all@2x"];
    [self.tabBar addSubview:selectedImage];
    
    
    
//    利用自己创建的button子类来创建按钮
    for (int i = 0; i<5; i++) {
        CGRect frame = CGRectMake(i*width, 0, width, height);
        MyButton *button = [[MyButton alloc]initWithFrame:frame withImageName:imageArray[i] withTitle:nameArray[i]];
        NSLog(@"%@",nameArray[i]);
        button.tag = i;
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.tabBar addSubview:button];
        
    
        
    }
    
    
}

- (void)buttonAction:(MyButton *)button
{
    
    
    NSInteger index = button.tag;
    self.selectedIndex = index;
 
    
    
    [UIView beginAnimations:nil context:nil];
    
    [UIView setAnimationDuration:0.3];
    
    //按钮图片的位置改变
    selectedImage.center = button.center;
    
    
    [UIView commitAnimations];
    
    
}

//创建子控件
- (void)_creatSubView
{
    NSArray *nameArray = [NSArray array];
    nameArray = @[@"首页",@"新闻",@"Top250",@"影院",@"更多"];
    
    //创建第三层viewcontroller
    MainViewController *vc1 = [[MainViewController alloc]init];
    NewsViewController *vc2 = [[NewsViewController alloc]init];
    TopViewController *vc3 = [[TopViewController alloc]init];
    CinemaViewController *vc4 = [[CinemaViewController alloc]init];
    MoreViewController *vc5 = [[MoreViewController alloc]init];
    
    
    NSMutableArray *vcArray = [NSMutableArray arrayWithObjects:vc1,vc2,vc3,vc4,vc5, nil];

    
    
    NSMutableArray *navArray = [NSMutableArray array];
    
    //创建第二层navigationViewController
    for (int i = 0; i<5; i++) {
        
        BaseNavigationController *nav = [[BaseNavigationController alloc]initWithRootViewController:vcArray[i]];
        [navArray addObject:nav];
        
        UIViewController *vc = [[UIViewController alloc]init];
        vc = vcArray[i];
        vc.title = nameArray[i];
        
    }
    self.viewControllers = navArray;
    
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
